package com.umerclass.ui.anmo;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.umerclass.AppDroid;
import com.umerclass.R;
import com.umerclass.bean.AddbankBean;
import com.umerclass.bean.anmo.BankCardInfoBean;
import com.umerclass.bean.anmo.BankListBean;
import com.umerclass.bean.anmo.PerformanceListBean;
import com.umerclass.frame.model.InfoResult;
import com.umerclass.frame.model.MsgBean;
import com.umerclass.frame.okhttp.OkEntityListRequest;
import com.umerclass.frame.okhttp.OkEntityRequest;
import com.umerclass.frame.okhttp.OkSimpleRequest;
import com.umerclass.frame.ui.activity.BaseActivity;
import com.umerclass.utils.Constants;
import com.umerclass.utils.EventConstants;

import butterknife.BindView;
import butterknife.OnClick;

public class BangDingCardActivity extends BaseActivity {

    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_card)
    EditText etCard;
    @BindView(R.id.et_card_address)
    EditText etCardAddress;
    @BindView(R.id.et_card_yinhang)
    TextView etCardYinhang;
    @BindView(R.id.et_card_type)
    TextView etCardType;
    @BindView(R.id.btn)
    Button btn;
    private String cardNum = "";
    private String name = "";
    private String cardAddress = "";
    private String bankName = "";
    private boolean clcik = false;
    private BankCardInfoBean bankCardInfoBean = new BankCardInfoBean();
    private String cardType = "";

    @Override
    public boolean isImmersionBarEnabled() {
        return false;
    }

    @Override
    public boolean isSupportSwipeBack() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_anmo_bang_ding_card;
    }

    @Override
    public void init() {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(getResources().getColor(R.color.white));

        etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(TextUtils.isEmpty(s.toString())){
                    name = "";
                }else {
                    name = s.toString();
                }
                setClickOne();
            }
        });

        etCard.addTextChangedListener(new TextWatcher() {
            int beforeLength;
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                beforeLength = s.length();

            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            //一般我们都是在这个里面进行我们文本框的输入的判断，上面两个方法用到的很少
            @Override
            public void afterTextChanged(Editable s) {
                if(TextUtils.isEmpty(s.toString())){
                    cardNum = "";
                }else {
                    cardNum = s.toString().replaceAll(" " , "");
                    bankCardInfoBean.setTotalBankcode(cardNum);
//                    cardType = bankCardInfoBean.getCardType();//银行卡类型 比如：信用卡/储蓄卡
                    bankName = bankCardInfoBean.getBankName();//银行卡名称  比如：建设银行/招商银行
                    etCardYinhang.setText(bankName);
                }
                setClickOne();
                int length = s.toString().length();
                boolean b = s.toString().endsWith(" ");
                if (beforeLength < length) {  //判断输入状态
                    if (length == 4 || length == 9 || length == 14) {
                        etCard.setText(new StringBuffer(s).insert(length, " ").toString());

                    } else if (length == 5 || length == 10 || length == 15) {//另一种情况，手动删除空格再次输入后
                        if (!b) {
                            etCard.setText(new StringBuffer(s).insert(length - 1, " ").toString());
                        }

                    }
                } else { //删除状态
                    if (b) {
                        etCard.setText(new StringBuffer(s).delete(length - 1, length).toString());
                    }
                }
                //设置指针选中位置
                etCard.setSelection(etCard.getText().toString().length());
            }
        });

        etCardAddress.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(TextUtils.isEmpty(s.toString())){
                    cardAddress = "";
                }else {
                    cardAddress = s.toString();
                }
                setClickOne();
            }
        });

    }

    @Override
    public void loadData() {

    }

    @OnClick({R.id.linear_back,R.id.btn})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                finish();
                break;
            case R.id.btn:
                if(clcik){
                    showProgress("正在请求...");
                    OkEntityRequest<AddbankBean> request = new OkEntityRequest<>(R.id.addbank,
                            Constants.addbank, AddbankBean.class);
                    request.addParams("userId", AppDroid.getInstance().getUserOtherBean().getUserId());
                    request.addParams("name", name);
                    request.addParams("bankNo", cardNum);
                    request.addParams("bankName", bankName);
                    request.addParams("bankAddress", cardAddress);
                    request.addHeadParams("userType",AppDroid.getInstance().getUserOtherBean().getUserType());
                    request.addHeadParams("userToken",AppDroid.getInstance().getUserOtherBean().getToken());
                    requestOkhttpEntity(request);
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {

        switch (what){
            case R.id.addbank:
                hideProgress();
                AddbankBean bean = (AddbankBean) t.getT();
                if(bean!=null){
                    MsgBean msgBean = new MsgBean();
                    msgBean.setFlag(EventConstants.addcardSuccess);
                    msgBean.setObject(msgBean);
                    getEventBus().post(msgBean);
                    finish();
                }
                break;
                default:break;
        }
    }

    @Override
    protected void onFail(int what, InfoResult t) {

    }

    private TextWatcher mTextWatcher = new TextWatcher() {
        int beforeLength;
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            beforeLength = s.length();

        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        //一般我们都是在这个里面进行我们文本框的输入的判断，上面两个方法用到的很少
        @Override
        public void afterTextChanged(Editable s) {
            if(TextUtils.isEmpty(s.toString())){
                cardNum = "";
            }else {
                cardNum = s.toString();
            }
            setClickOne();
            //cardNum银行卡号
            BankCardInfoBean bankCardInfoBean = new BankCardInfoBean(cardNum);
            if (bankCardInfoBean!=null){
                String cardType = bankCardInfoBean.getCardType();//银行卡类型 比如：信用卡/储蓄卡
                bankName = bankCardInfoBean.getBankName();//银行卡名称  比如：建设银行/招商银行
                etCardYinhang.setText(bankName);
            }
            int length = s.toString().length();
            boolean b = s.toString().endsWith(" ");
            if (beforeLength < length) {  //判断输入状态
                if (length == 4 || length == 9 || length == 14) {
                    etCard.setText(new StringBuffer(s).insert(length, " ").toString());

                } else if (length == 5 || length == 10 || length == 15) {//另一种情况，手动删除空格再次输入后
                    if (!b) {
                        etCard.setText(new StringBuffer(s).insert(length - 1, " ").toString());
                    }

                }
            } else { //删除状态
                if (b) {
                    etCard.setText(new StringBuffer(s).delete(length - 1, length).toString());
                }
            }
            //设置指针选中位置
            etCard.setSelection(etCard.getText().toString().length());
        }
    };

    private void setClickOne() {
        if (TextUtils.isEmpty(name)  || TextUtils.isEmpty(cardNum)
                || TextUtils.isEmpty(cardAddress) || TextUtils.isEmpty(bankName) ) {
            btn.setBackgroundResource(R.drawable.shape_one_normal);
            clcik = false;
        } else {
            btn.setBackgroundResource(R.drawable.shape_one_press);
            clcik = true;
        }
    }
}
